package com.vcc.web.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import com.vcc.core.annotations.RestPack;
import com.vcc.core.model.AmountData;
import com.vcc.core.model.CountData;
import com.vcc.data.entity.User;
import com.vcc.service.client.vo.statistics.StatisticsSumVo;
import com.vcc.service.client.vo.statistics.UserTransactionVo;
import com.vcc.service.service.*;
import com.vcc.web.core.util.UserUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("statisticsData")
public class StatisticsController {


    @Autowired
    private TransactionService transactionService;
    @Autowired
    private UserCardService userCardService;
    @Autowired
    private WalletDetailService walletDetailService;
    @Resource
    private UserService userService;
    @Resource
    private CardOrderService cardOrderService;


    @RestPack
    @GetMapping("getData")
    @SaCheckLogin
    public List<UserTransactionVo> getData(String startDate, String endDate) {
        Long userId = UserUtil.getId();
        return transactionService.sumCount(
                userId,
                UserUtil.getRole(),
                StringUtils.isBlank(startDate) ? DateUtil.offsetDay(new Date(), -30) : DateUtil.beginOfDay(DateUtil.parse(startDate)),
                StringUtils.isBlank(endDate) ? new Date() : DateUtil.endOfDay(DateUtil.parse(endDate))
                );
    }

    @RestPack
    @GetMapping("getUserData")
    @SaCheckLogin
//    @SaCheckPermission("user:statistics:view")
    public HashMap<String, Object> getUserData(Long userId, String startDate, String endDate) {
        Date dayBeginDate = StringUtils.isBlank(startDate) ? DateUtil.offsetDay(new Date(), -30) : DateUtil.parse(startDate);
        Date dayEndDate = StringUtils.isBlank(endDate) ? new Date() : DateUtil.parse(endDate);
        dayBeginDate = DateUtil.beginOfDay(dayBeginDate);
        dayEndDate = DateUtil.endOfDay(dayEndDate);
        //时间区间内开卡数
        //卡充值金额
        //卡转出金额
        //时间段内交易统计数据
        User user = UserUtil.get();
        UserUtil.baseVerifyUser(
                UserUtil.getRole(),
                user.getId(),
                userId,
                user.getParentId(),
                userService.getParentIdById(userId)
        );
        //开卡数统计
        List<CountData> countData = userCardService.countById(userId, dayBeginDate, dayEndDate);
        List<AmountData> recharge = cardOrderService.countAmountByUserId(userId, "RECHARGE", dayBeginDate, dayEndDate);
        List<AmountData> withdraw = cardOrderService.countAmountByUserId(userId, "WITHDRAW", dayBeginDate, dayEndDate);
        List<UserTransactionVo> transactionVos = transactionService.sumCount(userId, dayBeginDate, dayEndDate);
        return new HashMap<>() {
            {
                put("openCard", countData);
                put("recharge", recharge);
                put("withdraw", withdraw);
                put("transaction", transactionVos);
            }
        };
    }

    @RestPack
    @GetMapping("statistics")
    public StatisticsSumVo statistics() {
        Long userId = UserUtil.getId();
        // 1 卡充值 2 卡转出 3 账户充值 4 账户转出
        return StatisticsSumVo.builder()
                .accountRecharge(walletDetailService.sumAmountByUserId(userId, 3))
                .accountWithdraw(walletDetailService.sumAmountByUserId(userId, 4))
                .cardRecharge(walletDetailService.sumAmountByUserId(userId, 1))
                .cardWithdraw(walletDetailService.sumAmountByUserId(userId, 2))
                .build();
    }
}
